/**
 * 文件：InsertCostPanel.java
 * 说明：用于添加缴费信息
 * 作者：古通基
 * 时间：2011-09-14
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class InsertCostPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = 8322190950788771114L;
	private JLabel lno = new JLabel("学    号：");                                     //静态标签
	private JLabel lname = new JLabel("姓    名：");
	private JLabel lcharge = new JLabel("应缴费：");       
	private JLabel lshi = new JLabel("实缴费：");       
	private JLabel lqian = new JLabel("欠    费：");   

	private JTextField no = new JTextField("请输入五位学号",10);               //文本输入
	private JTextField name = new JTextField("",10); 
	String[] charges = {"4300","4800","5200","6400","7200","8000"};
	private JComboBox charge = new JComboBox(charges); 
	private JTextField shi = new JTextField("请输入实缴费",10); 
	private JTextField qian = new JTextField("",10); 
    
    private JButton insert = new JButton("添加");                                   //声明按钮
    private JButton reset = new JButton("重置");
    private JButton query = new JButton("查询");
    
    private JPanel JPcomon1 = new JPanel();                                         //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public InsertCostPanel()                                                                    //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lcharge.setHorizontalAlignment(SwingConstants.CENTER);
    	lshi.setHorizontalAlignment(SwingConstants.CENTER);
    	lqian.setHorizontalAlignment(SwingConstants.CENTER);
   	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lcharge);
    	JPcomon3.add(charge);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lshi);
    	JPcomon4.add(shi);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(lqian);
    	JPcomon5.add(qian);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(insert);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPbutton);
    	
    	name.setEditable(false);                                                                  //初始化数据保护
    	qian.setEditable(false);
    	
    	insert.addActionListener(this);                                                        //事件监听        
    	reset.addActionListener(this);
    	query.addActionListener(this);
    	no.addMouseListener(this);    	
    	charge.addMouseListener(this);
    	shi.addMouseListener(this);
    }    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
		}
		else if(e.getActionCommand().equals("添加"))
		{
			AddToDataBase();
		}
		else if(e.getActionCommand().equals("重置"))
		{
			no.setText("");
			name.setText("");
			shi.setText("");
			qian.setText("");
		}
	}
	
	private void QueryDataBase()
	{		
		ResultSet rs = null;
		try
		{
			BaseSQL stu = null;	
			stu = new BaseSQL();
			String sql = "select * from student where sno = '"+Integer.parseInt(no.getText())+"'"; 
			
		    rs = stu.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt(no.getText())==rs.getInt("sno"))
    			{
    				name.setText(rs.getString("sname"));		
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在学号为："+no.getText()+" 的学生","缴费错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	private void AddToDataBase()
	{	
		try
		{
			BaseSQL cost = null;
			cost = new BaseSQL();
			String sql = "insert into cost values('"+Integer.parseInt(no.getText())+"','"+name.getText()+"','"+charge.getSelectedItem()+"','"+shi.getText()+"')";
		    cost.executeDML(sql);	
		    qian.setText(Integer.toString(Integer.parseInt((String) charge.getSelectedItem())-Integer.parseInt(shi.getText().trim())));
		    JOptionPane.showMessageDialog(this, "缴费成功!", "缴费信息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
    public void mouseClicked(MouseEvent e) { }                                      //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}
    	else if(shi.getText().equals("请输入实缴费"))
    	{
    		shi.setText("");
    		this.setVisible(true);
    	}
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}
